package io.renren.modules.headmaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.constant.TimeConstant;
import io.renren.common.enums.AuditLevel;
import io.renren.common.enums.AuditState;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.generator.entity.AuditEntity;
import io.renren.modules.generator.entity.RestEntity;
import io.renren.modules.generator.service.AuditService;
import io.renren.modules.generator.service.RestService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;


/**
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-02-05 22:24:19
 */
@RestController
@RequestMapping("master/audit")
public class MasterAuditController {
    @Autowired
    private AuditService auditService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RestService restService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long classId = userEntity.getClassId();
        params.put("classId", classId);
        Page<Map<String, Object>> page = new Page<>(Long.valueOf(String.valueOf(params.get("page"))), Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = auditService.selectTeacherAuditList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);

    }

    @RequestMapping("/restList")
    public R restList(@RequestParam Map<String, Object> params) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long classId = userEntity.getClassId();
        params.put("classId", classId);
        Page<Map<String, Object>> page = new Page<>(Long.valueOf(String.valueOf(params.get("page"))), Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = auditService.studentRestList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{auditId}")
    public R info(@PathVariable("auditId") Integer auditId) {
        AuditEntity audit = auditService.getById(auditId);

        return R.ok().put("audit", audit);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AuditEntity audit) {
        auditService.save(audit);

        return R.ok();
    }

    /**
     * 班主任通过请假
     */
    @RequestMapping("/pass")
    @Transactional
    public R pass(@RequestBody Integer id) {
        AuditEntity auditEntity = auditService.getById(id);
        SysUserEntity userEntity = sysUserService.getById(auditEntity.getAuditPeopleId());
        SysUserEntity secretary = sysUserService.getById(userEntity.getCreateUserId());
        //请假条id
        Long auditRecordId = auditEntity.getAuditRecordId();
        //请假条详细信息
        RestEntity rest = restService.getById(auditRecordId);
        //请假开始时间
        Date startTime = rest.getStartTime();
        //请假结束时间
        Date endTime = rest.getEndTime();
        //判断请假时间是否大于三天
        boolean auditTime = endTime.getTime() - startTime.getTime() > TimeConstant.THREEDAY;

        AuditEntity audit = new AuditEntity();
        //设置审核人id
        audit.setAuditId(auditEntity.getAuditId());
        //设置老师审核的时间
        audit.setAuditTime(new Date());

        if (auditTime) {
            //设置书记审核级别
            audit.setAuditLevel(AuditLevel.SECRETARY.getLevel());
            //设置审核书记id
            audit.setAuditPeopleId(secretary.getUserId());
            //设置审核书记名称
            audit.setAuditPeopleName(secretary.getNickName());
        } else {
            audit.setAuditStatus(AuditState.ADOPT.getState());
        }

        auditService.updateById(audit);

        return R.ok();
    }

    /**
     * 班主任驳回请假
     */
    @RequestMapping("/reject")
    public R reject(@RequestBody Integer id) {
        AuditEntity auditEntity = new AuditEntity();
        auditEntity.setAuditId(id.longValue());
        auditEntity.setAuditTime(new Date());
        //设置审核状态为驳回
        auditEntity.setAuditStatus(AuditState.REJECT.getState());
        auditService.updateById(auditEntity);

        return R.ok();
    }

}
